//
//  ActivateViewController.m
//  JiuLe
//
//  Created by xcq on 15/11/6.
//  Copyright © 2015年 Fine. All rights reserved.
//
#import "Common.h"
#import "JlUserManager.h"
#import "BaseNavController.h"
#import "ActivateFirstCell.h"
#import "ActivateDetailCell.h"
#import "MallViewController.h"
#import "LoginViewController.h"
#import "CYLTabBarController.h"
#import "MyZoneViewController.h"
#import "ServiceViewController.h"
#import "MessageViewController.h"
#import "ActivateViewController.h"
#import "HomePageViewController.h"
#import "ActivateViewController.h"
#import "SideMenuContentViewController.h"

@interface ActivateViewController () <UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIButton *activateBtn;
@property (strong, nonatomic) NSMutableDictionary *inputContent;
@end

@implementation ActivateViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"激活";
    [self showNavBarWithRightTitle:nil
                    andRightAction:nil
                     andLeftAction:@selector(backTo)
                 andRightBtnIsShow:NO];
    [self setUpTable];
}

- (void)backTo {
    LoginViewController *login = getStoryOfControllerInstance(@"LoginViewController");
    self.view.window.rootViewController = login;
}

- (void)setUpTable {
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tableViewSpaceClick)]];
    [self SeparatorForTableView:_tableView];
}

- (void)tableViewSpaceClick {
    [self.view endEditing:YES];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (_inputContent == nil) _inputContent = [NSMutableDictionary new];
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    __block NSString *content = nil;
    __block NSInteger _index = -1;
    if (indexPath.section == 0) {
        ActivateFirstCell *firstCell = [[[NSBundle mainBundle] loadNibNamed:@"ActivateFirstCell"
                                                                      owner:self
                                                                    options:nil] lastObject];
        firstCell.data = [NSDictionary new];
        firstCell.SequenceNum.text =  [JlUserManager shareInstance].userInfo.pid;
        firstCell.endEditAction = ^(NSString *text , NSInteger index) {
            content = [text copy];
            if (index == 1) {
                [_inputContent setObject:content forKey:@"name"];
            } else {
                [_inputContent setObject:content forKey:@"phone"];
            }
        };
        if (_inputContent.allValues.count > 0) {
            firstCell.nickName.text = [_inputContent customObjectForkey:@"name"];
            firstCell.phoneNum.text = [_inputContent customObjectForkey:@"phone"];
        }
        
        return firstCell;
    } else {
        ActivateDetailCell *detail = [[[NSBundle mainBundle] loadNibNamed:@"ActivateDetailCell"
                                                                    owner:self
                                                                  options:nil] lastObject];
        detail.endEditAction = ^(NSString *text , NSInteger index) {
            content = [text copy];
            _index = index;
            if (indexPath.section == 1) {
                if (index == 1) {
                    [_inputContent setObject:content forKey:@"name1"];
                } else {
                    [_inputContent setObject:content forKey:@"phone1"];
                }
            } else {
                if (index == 1) {
                    [_inputContent setObject:content forKey:@"name2"];
                } else {
                    [_inputContent setObject:content forKey:@"phone2"];
                }
            }
        };
        NSString *objNameKey = nil;
        NSString *objPhoneKey = nil;
        if (indexPath.section == 1) {
            detail.ContactPersonNumLb.text = @"紧急联系人1";
            objNameKey=@"name1";
            objPhoneKey=@"phone1";
        } else {
            detail.ContactPersonNumLb.text = @"紧急联系人2";
            objNameKey=@"name2";
            objPhoneKey=@"phone2";
        }
        if (_inputContent.allValues.count > 0) {
            detail.nickNameLb.text = [_inputContent customObjectForkey:objNameKey];
            detail.phoneNumLb.text = [_inputContent customObjectForkey:objPhoneKey];
        }
        detail.data = [NSDictionary new];
        return detail;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 130;
    } else {
        return 135;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}

- (IBAction)activateEvent:(id)sender {
    ActivateFirstCell *firstCell = [_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0
                                                                                        inSection:0]];
    ActivateDetailCell *detailOneCell = [_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0
                                                                                             inSection:1]];
    ActivateDetailCell *detailTwoCell = [_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0
                                                                                             inSection:2]];
    if (firstCell.SequenceNum.text.length == 0) {
        [SVProgressHUD showErrorWithStatus:@"序列号不能为空"];
        return;
    } else if (firstCell.nickName.text.length == 0) {
        [SVProgressHUD showErrorWithStatus:@"姓名不能为空"];
        return;
    } else if (firstCell.phoneNum.text.length == 0 || firstCell.phoneNum.text.length < 11) {
        [SVProgressHUD showErrorWithStatus:@"号码长度有误"];
        return;
    } else if (detailOneCell.nickNameLb.text.length == 0) {
        [SVProgressHUD showErrorWithStatus:@"紧急联系人1姓名不能为空"];
        return;
    } else if (detailOneCell.phoneNumLb.text.length == 0 || detailOneCell.phoneNumLb.text.length < 11) {
        [SVProgressHUD showErrorWithStatus:@"紧急联系人1电话号码长度有误"];
        return;
    } else if (detailTwoCell.nickNameLb.text.length == 0) {
        [SVProgressHUD showErrorWithStatus:@"紧急联系人2姓名不能为空"];
        return;
    } else if (detailTwoCell.phoneNumLb.text.length == 0 || detailTwoCell.phoneNumLb.text.length < 11) {
        [SVProgressHUD showErrorWithStatus:@"紧急联系人2电话号码长度有误"];
        return;
    }

    [InterfaceNetManager RequestActivateDevice:^(BOOL isSucceed, NSString *message, id data, NSError *error) {
        if (isSucceed) {
            [SVProgressHUD showSuccessWithStatus:@"激活成功"];
            [[NSUserDefaults standardUserDefaults] setObject:@(YES) forKey:UserDeviceIsActivity];
            [self performSelector:@selector(setRootController)
                       withObject:nil
                       afterDelay:1.f];
        } else {
            [[NSUserDefaults standardUserDefaults] setObject:@(NO) forKey:UserDeviceIsActivity];
            [SVProgressHUD showErrorWithStatus:@"激活失败"];
        }
    } andParameter:@{@"indexCode": [firstCell.SequenceNum text],
                     @"username":[_inputContent customObjectForkey:@"name"],
                     @"phoneNumber":[_inputContent customObjectForkey:@"phone"],
                     @"name1":[_inputContent customObjectForkey:@"name1"],
                     @"phoneNumber1":[_inputContent customObjectForkey:@"phone1"],
                     @"name2":[_inputContent customObjectForkey:@"name2"],
                     @"phoneNumber2":[_inputContent customObjectForkey:@"phone2"]}];
    
}

- (void)setRootController {
    //跳转首页
    JlUserManager *manager = [JlUserManager shareInstance];
    CYLTabBarController *tabbar = [[CYLTabBarController alloc] init];
    [self setUpTabBar:tabbar];
    manager.baseTab=tabbar;
    SideMenuContentViewController *personal = getStoryOfControllerInstance(@"SideMenuContentViewController");
    BaseNavController *base = [[BaseNavController alloc] initWithRootViewController:personal];
    personal.navigationController.navigationBarHidden = YES;
    RESideMenu *sideMenu              = [[RESideMenu alloc] init];
    sideMenu.leftMenuViewController   = base;
    sideMenu.contentViewController    = tabbar;
    sideMenu.menuPreferredStatusBarStyle = UIStatusBarStyleLightContent;
    sideMenu.backgroundImage       = [UIImage imageNamed:@"bg"];
    sideMenu.contentViewShadowColor   = [UIColor blackColor];
    sideMenu.contentViewShadowOffset  = CGSizeMake(0, 0);
    sideMenu.contentViewShadowOpacity = 0.6;
    sideMenu.contentViewShadowRadius  = 12;
    sideMenu.contentViewShadowEnabled = YES;
    sideMenu.interactivePopGestureRecognizerEnabled = YES;
    sideMenu.contentViewScaleValue = 0.9;
    sideMenu.panGestureEnabled        = YES;
    sideMenu.panFromEdge              = NO;
    manager.menu = sideMenu;
    self.view.window.rootViewController = manager.menu;
}

- (void)setUpTabBar:(CYLTabBarController *)tabbar {
    
    HomePageViewController *home = getStoryOfControllerInstance(@"HomePageViewController");
    BaseNavController *baseNav1 = [[BaseNavController alloc] initWithRootViewController:home];
    
    MallViewController *mall = getStoryOfControllerInstance(@"MallViewController");
    
    ServiceViewController *service = getStoryOfControllerInstance(@"ServiceViewController");
    BaseNavController *baseNav3 = [[BaseNavController alloc] initWithRootViewController:service];
    
    MessageViewController *message = getStoryOfControllerInstance(@"MessageViewController");
    BaseNavController *baseNav4 = [[BaseNavController alloc] initWithRootViewController:message];
    
    MyZoneViewController *myZone = getStoryOfControllerInstance(@"MyZoneViewController");
    
    [self customizeTabBarForController:tabbar];
    
    // set the text color for unselected state
    // 普通状态下的文字属性
    NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
    normalAttrs[NSForegroundColorAttributeName] = [UIColor blackColor];
    normalAttrs[NSFontAttributeName] = [UIFont fontWithName:kfontName size:14.];
    
    // set the text color for selected state
    // 选中状态下的文字属性
    NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
    selectedAttrs[NSForegroundColorAttributeName] = [UIColor colorWithHex:0x217AB9];
    selectedAttrs[NSFontAttributeName] = [UIFont fontWithName:kfontName size:14.];
    // set the text Attributes
    // 设置文字属性
    UITabBarItem *tabBar = [UITabBarItem appearance];
    [tabBar setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
    [tabBar setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
    
    [tabbar setViewControllers:@[
                                 baseNav1,
                                 mall,
                                 baseNav3,
                                 baseNav4,
                                 myZone,
                                 ]];
    
}

- (void)customizeTabBarForController:(CYLTabBarController *)tabBarController {
    
    NSDictionary *dict1 = @{
                            CYLTabBarItemTitle : @"首页",
                            CYLTabBarItemImage : @"nav_home",
                            CYLTabBarItemSelectedImage : @"nav_home_cur",
                            };
    NSDictionary *dict2 = @{
                            CYLTabBarItemTitle : @"商城",
                            CYLTabBarItemImage : @"nav_shop",
                            CYLTabBarItemSelectedImage : @"nav_shop",
                            };
    NSDictionary *dict3 = @{
                            CYLTabBarItemTitle : @"服务",
                            CYLTabBarItemImage : @"nav_service",
                            CYLTabBarItemSelectedImage : @"nav_service",
                            };
    NSDictionary *dict4 = @{
                            CYLTabBarItemTitle : @"消息",
                            CYLTabBarItemImage : @"nav_mail",
                            CYLTabBarItemSelectedImage : @"nav_mail"
                            };
    NSDictionary *dict5 = @{
                            CYLTabBarItemTitle : @"我的",
                            CYLTabBarItemImage : @"nav_me",
                            CYLTabBarItemSelectedImage : @"nav_me"
                            };
    
    NSArray *tabBarItemsAttributes = @[
                                       dict1,
                                       dict2,
                                       dict3,
                                       dict4,
                                       dict5
                                       ];
    tabBarController.tabBarItemsAttributes = tabBarItemsAttributes;
    
    UIColor *titleHighlightedColor = [UIColor darkGrayColor];
    [[UITabBarItem appearance] setTitleTextAttributes:
     [NSDictionary dictionaryWithObjectsAndKeys: titleHighlightedColor, NSForegroundColorAttributeName,iOS8Font(16.f), NSFontAttributeName, nil] forState:UIControlStateSelected];
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}



@end
